﻿
using System.Collections.Generic;
using Red.Controls.TimeLine.Core;
using Red.Controls.ViewModels;
using Red.Core;

namespace Red.Controls.TimeLine.ViewModels
{
    /// <summary>
    /// Base object for all ViewModels used by TimeLine component
    /// </summary>
    public class TimeLineItemBase : ViewModel
    {
        #region Data

        private readonly TimeLineId _id;

        private TimeLineControler _controler;
        private DataItemViewModel _dataModel;

        #endregion

        #region C-tor

        protected TimeLineItemBase( TimeLineId id, TimeLineControler controler )
        {
            _id = id;
            _controler = controler;
        }

        #endregion

        #region Properties

        public TimeLineId Id
        {
            get { return _id; }
        }

        public virtual IEnumerable<RedMenuDataItem> Menu
        {
            get { return new List<RedMenuDataItem>(); }
        }

        public TimeLineControler TimeLineControler
        {
            get { return _controler; }
        }

        public DataItemViewModel DataModel
        {
            protected get { return _dataModel; }
            set
            {
                if ( _dataModel != value )
                {
                    if ( _dataModel != null )
                    {
                        // Remove registred properties callback
                        _dataModel.Deinitialize();
                    }

                    _dataModel = value;
                    if ( _dataModel != null )
                    {
                        // Register properties and callbacks
                        OnDataModelChanged();
                        _dataModel.InitializeProperties();
                    }
                }
            }
        }

        public string Name
        {
            get { return DataModel?.GetValue( "Name" ) ?? string.Empty; }
            set
            {
                if ( DataModel != null )
                    DataModel.SetValue( "Name", value );
            }
        }

        public string Type
        {
            get { return DataModel?.GetValue( "Value" ) ?? string.Empty; }
        }

        #endregion

        #region Public Methods

        public virtual void RecalculateVisualsProperties()
        {
        }

        #endregion

        #region Public Methods

        protected void OnDefaultPropertyChange( string propertyName, string value )
        {
            OnPropertyChanged( propertyName );
        }

        protected virtual void OnDataModelChanged()
        {
            DataModel.RegisterProperty( "Name", "name", OnDefaultPropertyChange );
            DataModel.RegisterProperty( "Type", "type", OnDefaultPropertyChange );
        }

        #endregion
    }
}
